export default {
  state: () => ({
    graphFiles: [],
    currentGraphFile: null,
    graph: null,
    stencil: null,
    // graphOptions里记录无法通过x6 api获取到的选项
    graphOptions: {
      grid: {
        type: 'dot',
        args: {
          color: '#a0a0a0'
        }
      },
      background: {
        color: '#333b50'
      }
    },

    canRedo: false,
    canUndo: false,

    canCut: false,
    canCopy: false,
    canPaste: false,
    canDelete: false,

    // 是否需要刷新graph属性
    refreshGraphProp: 0,

    // 是否需要刷新节点属性
    refreshNodeProp: 0,

    // 画布中当前操作的节点
    operateNode: undefined,

    newGraphFileFlag: 0,
    openGraphFileFlag: 0
  }),

  mutations: {
    addGraphTab(state, graphFile) {
      state.graphFiles.push({
        id: graphFile.id,
        title: graphFile.name
      })
    },

    removeGraphTab(state, fileId) {
      const index = state.graphFiles.findIndex(value => {
        return value.id === fileId
      })
      if (index !== -1) {
        state.graphFiles.splice(index, 1)
      }
    },

    setCurrentGraphFile(state, graphFile) {
      state.currentGraphFile = graphFile
    },

    setGraph(state, graph) {
      state.graph = graph
    },

    setStencil(state, stencil) {
      state.stencil = stencil
    },

    updateGraphGridOption(state, option) {
      state.graphOptions.grid.type = option.type
      state.graphOptions.grid.args.color = option.color
    },

    updateGraphBackgroundOption(state, color) {
      state.graphOptions.background.color = color
    },

    setRedoUndoState(state, histroyState) {
      state.canRedo = histroyState.canRedo
      state.canUndo = histroyState.canUndo
    },

    setGraphSelectedState(state) {
      var cells = state.graph.getSelectedCells()
      state.canCut = cells.length !== 0
      state.canCopy = cells.length !== 0
      state.canPaste = !state.graph.isClipboardEmpty()
      state.canDelete = cells.length !== 0
    },

    refreshNodeProp(state, node) {
      state.operateNode = node
      state.refreshNodeProp++
      if (state.refreshNodeProp > 10000) {
        state.refreshNodeProp = 0
      }
    },

    setOperateNode(state, node) {
      state.operateNode = node
    },

    newGraphFile(state) {
      state.newGraphFileFlag++
    },

    openGraphFile(state) {
      state.openGraphFileFlag++
    }
  },

  actions: {}
}
